<?php
/**
 * @file
 * @package mileage
 * @version $Id$
**/

if (!defined('XOOPS_ROOT_PATH')) {
	exit;
}

require_once MILEAGE_TRUST_PATH . '/class/AbstractListAction.class.php';

/**
 * Mileage_RecListAction
**/
class Mileage_SaleListAction extends Mileage_AbstractListAction
{
	private $_buy_mile = NULL;
//	private $_totalPoint = 0;
	private $_msg = NULL;
	private $_buy_mile_id = NULL;

	/**
	 * &_getHandler
	 * 
	 * @param   void
	 * 
	 * @return  Mileage_MileHandler
	**/
	protected function &_getHandler()
	{
		$handler =& $this->mAsset->getObject('handler', 'mile');
		return $handler;
	}

	public function hasPermission()
	{
		$root =& XCube_Root::getSingleton();
		if (is_object($root->mContext->mXoopsUser)) {
			return TRUE;
		} else {
			return FALSE;
		}
	}

	/**
	 * getDefaultView
	 * 
	 * @param   void
	 * 
	 * @return  Enum
	**/
	public function getDefaultView()
	{
		$root =& XCube_Root::getSingleton();
		$this->_uid = $root->mContext->mXoopsUser->get('uid');

		$this->mFilter =& $this->_getFilterForm();
		$this->mFilter->fetch();
		$handler =& $this->_getHandler();
		$this->mObjects =& $handler->getObjects($this->mFilter->getCriteria());

		// From Block
	//	$this->_msg = NULL;
		if (isset($_SESSION['MILEAGE_BUY_POINT_MSG'])) {
			$this->_msg = $_SESSION['MILEAGE_BUY_POINT_MSG'];
			unset($_SESSION['MILEAGE_BUY_POINT_MSG']);
		}
	//	$this->_buy_mile_id = NULL;
		if (isset($_SESSION['MILEAGE_BUY_POINT_MILE_ID'])) {
			$this->_buy_mile_id = $_SESSION['MILEAGE_BUY_POINT_MILE_ID'];
			unset($_SESSION['MILEAGE_BUY_POINT_MILE_ID']);
		}

		// check can buy
		$recHandler =& $this->mAsset->getObject('handler', 'rec');
		foreach ($this->mObjects as $key => $obj) {
			$mile_id = $obj->get('mile_id');
			$interval = $obj->get('interval');
			$this->mObjects[$key]->canBuy = Mileage_Utils::notExistsMileIDinRec($recHandler, $mile_id, $interval, $this->_uid, $this->mAsset->mDirname);
			if (!empty($this->_buy_mile_id) && $this->_buy_mile_id == $mile_id && $this->mObjects[$key]->canBuy) {
				$this->_buy_mile = $this->mObjects[$key];
			}
		}

		$this->_recActionForm =& $this->mAsset->getObject('form', 'rec', FALSE, 'edit');

		// LOGIN MILE
	//	$this->_loginMile = $handler->getObjects(new Criteria('url', 'login'));

		return MILEAGE_FRAME_VIEW_INDEX;
	}

	/**
	 * &_getFilterForm
	 * 
	 * @param   void
	 * 
	 * @return  Mileage_SaleFilterForm
	**/
	protected function &_getFilterForm()
	{
		// $filter =& new Mileage_SaleFilterForm();
		$filter =& $this->mAsset->getObject('filter', 'sale', FALSE);// 3rd arg is FALSE(not admin) 
		$filter->prepare($this->_getPageNavi(), $this->_getHandler());
		return $filter;
	}

	/**
	 * _getBaseUrl
	 * 
	 * @param   void
	 * 
	 * @return  string
	**/
	protected function _getBaseUrl()
	{
		$url_approach = $this->mModule->getModuleConfig('url_approach');
		return Mileage_Utils::makeBaseUrl($this->mAsset->mDirname, 'SaleList', $url_approach);
	}

	/**
	 * executeViewIndex
	 * 
	 * @param   XCube_RenderTarget  &$render
	 * 
	 * @return  void
	**/
	public function executeViewIndex(&$render)
	{
		$dirname = $this->mAsset->mDirname;
		XCube_DelegateUtils::call('Module.'. $dirname .'.Event.SaleList.executeViewIndex', new XCube_Ref($this->mObjects), new XCube_Ref($render));

		$render->setTemplateName($dirname . '_sale_list.html');
		$render->setAttribute('messeage', $this->_msg);
		$render->setAttribute('buyMileId', $this->_buy_mile_id);
		$render->setAttribute('buyMile', $this->_buy_mile);
		$render->setAttribute('dirname', $dirname);
		$render->setAttribute('objects', $this->mObjects);
		$render->setAttribute('pageNavi', $this->mFilter->mNavi);
		$render->setAttribute('actionForm', $this->_recActionForm);
		$render->setAttribute('uid', $this->_uid);
		$render->setAttribute('isMobile', Mileage_Utils::checkMobile());
		$render->setAttribute('totalPoint', Mileage_Utils::totalMileByUid($dirname));
		$render->setAttribute('xoops_pagetitle', _MD_MILEAGE_LANG_SALE_LIST . ' : ' . _MD_MILEAGE_LANG_MILEAGE);
		$render->setAttribute('xoops_breadcrumbs', Mileage_Utils::breadCrumbs($dirname, _MD_MILEAGE_LANG_SALE_LIST));
	/*	if (count($this->_loginMile) > 0) {
			$render->setAttribute('login', TRUE);
		}*/

		// CSS
		$css = XOOPS_URL . '/modules/' . $dirname . '/style.css';
		$moduleHeader = $render->getAttribute('xoops_module_header');
		$moduleHeader .= '<link rel="stylesheet" type="text/css" media="screen" href="'. $css .'" />';
		$render->setAttribute('xoops_module_header', $moduleHeader);
	}
}

